package org.aplikator.client.shared.rpc.marshaller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.aplikator.client.shared.descriptor.ClientDescriptorBase;
import org.aplikator.client.shared.rpc.marshaller.DataMarshallingUtils.DataType;
import org.jboss.errai.marshalling.client.api.Marshaller;
import org.jboss.errai.marshalling.client.api.MarshallingSession;
import org.jboss.errai.marshalling.client.api.json.EJArray;
import org.jboss.errai.marshalling.client.api.json.EJObject;
import org.jboss.errai.marshalling.client.api.json.EJValue;


public class MarshallingUtils {
	

	/*public static StringBuilder marshallData(StringBuilder input, Serializable value) {
		DataType lookpup = DataMarshallingUtils.DataType.lookup(value);
		if (lookpup != null) {
			return lookpup.marshall(input, value);
		} else throw new UnsupportedOperationException("unsupported type !");
	}*/
	
	
	/** marshall default map */
	public static StringBuilder mapMarshall(StringBuilder input, Map o, MarshallingSession ctx, String encName) {
		input.append('{');
		Marshaller<Object> marshaller = ctx.getMarshallerInstance(encName);
		Object[] keys = o.keySet().toArray();
		for (int i = 0,ll=keys.length; i < ll; i++) {
			if (i>0) input.append(',');
			key(keys[i].toString(), input).append(':').append(marshaller.marshall(o.get(keys[i]), ctx));
		}
		input.append('}');
		return input;
	}
	
	/** marshall default list */
	public static StringBuilder listMarshall(StringBuilder input, List o, MarshallingSession ctx, String encName) {
		input.append('[');
		Marshaller<Object> marshaller = ctx.getMarshallerInstance(encName);
		for (int i = 0,ll=o.size(); i < ll; i++) {
			if (i > 0) input.append(',');
			input.append(marshaller.marshall(o.get(i), ctx));
		}
		input.append(']');
		return input;
	}

	
	public static StringBuilder clientDescriptorBase(StringBuilder input, ClientDescriptorBase cdb) {
		StringBuilder m = string("id", cdb.getId(), input).append(',');
		m = string("localizedName", cdb.getLocalizedName(),input);
		return m;
	}
	
	public static StringBuilder key(String key, StringBuilder input) {
		return input.append('"').append(key).append('"');
	}
	
	public static StringBuilder number(String key, Number number, StringBuilder input) {
		return key(key,input).append(':').append(number);
	}
	
	public static String escape(String val) {
		StringBuilder builder = new StringBuilder();
		char[] chrs = val.toCharArray();
		for (int i = 0; i < chrs.length; i++) {
			char ch = chrs[i];
			switch(ch) {
				case '\n':
					builder.append('\\').append('n');
				break;
				case '"':
					builder.append('\\').append('"');
				break;
				case '\\':
					builder.append('\\').append('\\');
				break;
				default:
					builder.append(ch);
				break;
			}
		}
		return builder.toString();
	}
	
	public static StringBuilder string(String key, String str, StringBuilder input) {
		if (str != null) {
			return key(key,input).append(':').append('"').append(escape(str)).append('"');
		} else {
			return key(key,input).append(':').append("null");
		}
	}
	public static StringBuilder bool(String key, boolean val, StringBuilder input) {
		return key(key,input).append(':').append(val);
	}
	

	public static StringBuilder nullval(String key, StringBuilder input) {
		return key(key, input).append(':').append("null");
	}
	
	public static void main(String[] args) {
		StringBuilder builder = new StringBuilder("prvniradek \n "
				+ "druhyradek \"\n"
				+ "treti radek \n");
		
		System.out.println(escape(builder.toString()));
		
	}
}
